<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
if($_RQ['op']=='list'){	
	$params=array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']);
	$orderby='id desc';
	$type=intval($_RQ['type']);
	if($type){
		$params['sendtype']=$type;
		$return['params']['type']=$type;
	}	
	if(isset($_RQ['status'])){
		if($_RQ['status']=='other'){
			$orderby='daytime asc';
			$status='0';
		}else{
			$status=$_RQ['status'];
		}
		$params['status']=$status;
		$return['params']['status']=$status;
	}
	$order_id=intval($_RQ['order_id']);
	if($order_id){
		$params['order_id']=$order_id;
		$return['params']['order_id']=$order_id;
	}
	$buymid=intval($_RQ['buymid']);
	if($buymid){
		$params['buymid']=$buymid;
		$return['params']['buymid']=$buymid;
	}
	$credit_types=credit_types();
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_peimilk_sendlog',$params,array($pindex,$psize),$total,'','',$orderby);
	$work_weekarray=array('日','一','二','三','四','五','六');
	if($list){
		foreach($list as &$item){
			$item['goods']=json_decode($item['goods'],true);
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
			$item['is_addtitle']=$item['is_add']?'获得':'消费';
			$item['ordersn']=pdo_getcolumn('jy_weishop_order',array('id'=>$item['order_id']),'ordersn');
			$week = date('w', strtotime($item['day']));
			$item['week'] = '星期'.$work_weekarray[$week];
			$item['dayweek'] = $item['day'].$item['week'];
			$order=pdo_get('jy_weishop_order',array('id'=>$item['order_id']),array('ordersn','price','merchid','address','addressid'));
			//用户地址
			$order['address']=json_decode($order['address'],true);
			if($order['address'] && ($order['address']['name'] || $order['address']['realname'])){
				$address=$order['address'];
			}else{
				$address=pdo_get('core_address',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['addressid']));
			}
			if(!is_array($address['pcd'])){
				$address['pcd']=json_decode($address['pcd'],true);
			}
			$item['order_out']=$address;
			if($item['mid']){
				$item['peimember']=pdo_get('jy_peimilk_member',array('mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']),array('title','mobile','level_id'));
				$item['peimember']['nickname']=$item['peimember']['title'];
				$item['peimember']['level']=pdo_getcolumn('jy_peimilk_level',array('id'=>$item['peimember']['level_id'],'uniacid'=>$_SESSION['uniacid']),'title');
				$item['peimember']['level']=$item['peimember']['level']?:'普通等级';
			}
			$item['get_money']=$this->model('model')->get_peifee($order,$item['mid'],$item['goods']);
			$item['order']=$order;
			$item['member']=cfc('core')->member_show($item['buymid']);
			$item['goodsinfo']='';
			foreach($item['goods'] as $gv){
				$gv['title']=cfc('tools')->substr($gv['title'],0,6);
				$item['goodsinfo'] .=$gv['title'];
				if($gv['selespename']){
					$item['goodsinfo'] .='['.$gv['selespename'].']';
				}
				if($gv['leititle']){
					$item['goodsinfo'] .='['.$gv['leititle'].']';
				}
				$item['goodsinfo'] .="(".$gv['num'].")；";
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>false,'list'=>array(
			array('name'=>'buymid','title'=>'客户ID','type'=>'string'),
			array('name'=>'member','title'=>'客户','type'=>'member'),
			array('name'=>'dayweek','title'=>'日期','type'=>'string'),
			array('name'=>'peimember','title'=>'配送员','type'=>'member'),
			array('name'=>'sendtype','title'=>'配送类型','type'=>'radio','options'=>'send_types'),
			array('name'=>'order.price','title'=>'订单金额','type'=>'string'),
			array('name'=>'goodsinfo','title'=>'商品详情','type'=>'string'),
			array('name'=>'get_money','title'=>'配送费','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'send_status'),
		),'action'=>array(
			array('type'=>'modal','op'=>'look','title'=>'查看详情'),
		)),
		'search'=>array(
			array('name'=>'status','width'=>10,'title'=>'状态','type'=>'radio','options'=>'send_status'),
			array('type'=>'choose','width'=>15,'action'=>'core/member.get_member/','name'=>'buymid','title'=>'选择会员'),
			array('name'=>'order_id','width'=>10,'title'=>'订单ID','type'=>'string'),
		),
	);
	$return['htmls']=$htmls;
	$return['send_status']=$this->send_status;
	$return['send_types']=$this->send_types;
	exi($return);
}
if($_RQ['op']=='look'){
	$item=$this->get('sendlog',array('id'=>intval($_RQ['id']),'uniacid'=>$_SESSION['uniacid']));
	if(!$item){
		exi('指定单不存在！','error');
	}
	$work_weekarray=array('日','一','二','三','四','五','六');
	$item['goods']=json_decode($item['goods'],true);
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
			$item['is_addtitle']=$item['is_add']?'获得':'消费';
			$item['ordersn']=pdo_getcolumn('jy_weishop_order',array('id'=>$item['order_id']),'ordersn');
			$week = date('w', strtotime($item['day']));
			$item['week'] = '星期'.$work_weekarray[$week];
			$item['dayweek'] = $item['day'].$item['week'];
			$order=pdo_get('jy_weishop_order',array('id'=>$item['order_id']),array('ordersn','price','merchid','address','addressid'));
			//用户地址
			$order['address']=json_decode($order['address'],true);
			if($order['address'] && ($order['address']['name'] || $order['address']['realname'])){
				$address=$order['address'];
			}else{
				$address=pdo_get('core_address',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['addressid']));
			}
			if(!is_array($address['pcd'])){
				$address['pcd']=json_decode($address['pcd'],true);
			}
			$item['order_out']=$address;
			if($item['mid']){
				$item['peimember']=pdo_get('jy_peimilk_member',array('mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']),array('title','mobile','level_id'));
				$item['peimember']['nickname']=$item['peimember']['title'];
				$item['peimember']['level']=pdo_getcolumn('jy_peimilk_level',array('id'=>$item['peimember']['level_id'],'uniacid'=>$_SESSION['uniacid']),'title');
				$item['peimember']['level']=$item['peimember']['level']?:'普通等级';
			}
			$item['get_money']=$this->model('model')->get_peifee($order,$item['mid'],$item['goods']);
			$item['order']=$order;
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['goodsinfo']='';
	foreach($item['goods'] as $gv){
				//$gv['title']=cfc('tools')->substr($gv['title'],0,6);
				$item['goodsinfo'] .=$gv['title'];
				if($gv['selespename']){
					$item['goodsinfo'] .='['.$gv['selespename'].']';
				}
				$item['goodsinfo'] .="(".$gv['num']."件)；\n";
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'look',
		'desc'=>'配送单详情',
		'look'=>array(
			array('name'=>'buymid','title'=>'客户ID','type'=>'string'),
			array('name'=>'member','title'=>'客户','type'=>'member'),
			array('name'=>'dayweek','title'=>'日期','type'=>'string'),
			array('name'=>'peimember','title'=>'配送员','type'=>'member'),
			array('name'=>'sendtype','title'=>'配送类型','type'=>'radio','options'=>'send_types'),
			array('name'=>'order.price','title'=>'订单金额','type'=>'string'),
			array('name'=>'goods','title'=>'商品详情','type'=>'array',
					'list'=>array(
						array('type'=>'string','name'=>'title','title'=>'商品名称'),
						array('type'=>'string','name'=>'selespename','title'=>'商品规格'),
						array('type'=>'string','name'=>'num','title'=>'商品数量'),
					),
			),
			array('name'=>'get_money','title'=>'配送费','type'=>'string'),
			array('name'=>'order_out.pcd','title'=>'地址','type'=>'pcd'),
			array('name'=>'order_out.address','title'=>'详细地址','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'send_status'),
		),
	);
	$return['send_status']=$this->send_status;
	$return['send_types']=$this->send_types;
	exi($return);
}
//报备
if($_RQ['op']=='baobei'){
	$post=get_postdata();
	$logid=intval($post['id']);
	if(!$logid){
		exi('订单不存在，请重新选择订单','error');
	}
	if($post['baobeitype']==3){
		$msg=$this->baobei_types3[$post['othertype']];
	}else{
		$msg=$this->baobei_types[$post['baobeitype']];
	}
	$sendlog=pdo_get('jy_peimilk_sendlog',array('id'=>$logid,'uniacid'=>$_SESSION['uniacid']));
	model('jy_peimilk/order')->add_baobei($sendlog,$msg,2);
	exi('报备成功！','','jy_peimilk/worker_order/list/');
}
//发起定期配送
if($_RQ['op']=='dingpei1'){
	$logid=intval($_RQ['id']);
	if(!$logid){
		exi('订单不存在，请重新选择订单','error');
	}
	$this->model('order')->sendlog1($logid,$day);
	exi('发起配送成功，请在指定时间段完成内配送！');
}
//发起定期配送
if($_RQ['op']=='dingpei2'){
	$logid=intval($_RQ['id']);
	if(!$logid){
		exi('订单不存在，请重新选择订单','error');
	}
	$this->model('order')->sendlog2($logid);
	exi('恭喜你完成一单！感谢你的努力！');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_peimilk_sendlog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}